package edu.gatech.asthma.ui_support.login;

import java.util.List;

import edu.gatech.asthma.ui_support.ProfileCustomizer;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * View adapter for the login screen
 */
public class ViewPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public ViewPagerAdapter(FragmentManager fm, List<Fragment> frags) {
    	super(fm);
        fragments = frags;
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
    
	@Override
	public Fragment getItem(int position) {
		return fragments.get(position);
	}
	
	/**
	 * Sets listener and appropriate behaviors for left arrow button of the viewpager
	 * @param v The button
	 * @param pager This ViewPagerAdapter's corresponding ViewPager
	 * @param customizer Holds currently selected color options. null for login screen
	 */
	public void setListenerLeft(View v, final ViewPager pager, final ProfileCustomizer customizer) {
		v.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	int i = pager.getCurrentItem();
            	int n = getCount();
            	if (i==0) pager.setCurrentItem(n-1);
            	else pager.setCurrentItem(i-1);
            	
            	if(customizer != null) {
            		customizer.setTintColor();
            		customizer.setAccentColor();
            	}
            }
        });
	}
	
	/**
	 * Sets listener and appropriate behaviors for right arrow button of the viewpager
	 * @param v The button
	 * @param pager This ViewPagerAdapter's corresponding ViewPager
	 * @param customizer Holds currently selected color options. null for login screen
	 */
	public void setListenerRight(View v, final ViewPager pager,final ProfileCustomizer customizer) {
		v.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	int i = pager.getCurrentItem();
            	int n = getCount();
            	pager.setCurrentItem((i+1)%n);
            	
            	if(customizer != null) {
            		customizer.setTintColor();
            		customizer.setAccentColor();
            	}
            }
        });
	}

}